// 事件类型枚举
export enum EventType {
  // 资源生命周期事件
  RESOURCE_CREATED = 'RESOURCE_CREATED', // 资源创建
  RESOURCE_UPDATED = 'RESOURCE_UPDATED', // 资源更新
  RESOURCE_DELETED = 'RESOURCE_DELETED', // 资源删除
  RESOURCE_ARCHIVED = 'RESOURCE_ARCHIVED', // 资源归档

  // 状态变更事件
  STATUS_CHANGED = 'STATUS_CHANGED', // 状态变更
  LIFECYCLE_STATUS_CHANGED = 'LIFECYCLE_STATUS_CHANGED', // 生命周期状态变更
  AVAILABILITY_STATUS_CHANGED = 'AVAILABILITY_STATUS_CHANGED', // 可用性状态变更
  VERSION_STATUS_CHANGED = 'VERSION_STATUS_CHANGED', // 版本状态变更
  CUSTOM_STATUS_CHANGED = 'CUSTOM_STATUS_CHANGED', // 自定义状态变更

  // 行为执行事件
  BEHAVIOR_EXECUTED = 'BEHAVIOR_EXECUTED', // 行为执行
  BEHAVIOR_STARTED = 'BEHAVIOR_STARTED', // 行为开始
  BEHAVIOR_COMPLETED = 'BEHAVIOR_COMPLETED', // 行为完成
  BEHAVIOR_FAILED = 'BEHAVIOR_FAILED', // 行为失败
  BEHAVIOR_CANCELLED = 'BEHAVIOR_CANCELLED', // 行为取消
  BEHAVIOR_TIMEOUT = 'BEHAVIOR_TIMEOUT', // 行为超时

  // 补偿事件
  COMPENSATION_TRIGGERED = 'COMPENSATION_TRIGGERED', // 补偿触发
  COMPENSATION_STARTED = 'COMPENSATION_STARTED', // 补偿开始
  COMPENSATION_COMPLETED = 'COMPENSATION_COMPLETED', // 补偿完成
  COMPENSATION_FAILED = 'COMPENSATION_FAILED', // 补偿失败

  // 属性变更事件
  PROPERTY_CHANGED = 'PROPERTY_CHANGED', // 属性变更
  PROPERTY_ADDED = 'PROPERTY_ADDED', // 属性添加
  PROPERTY_REMOVED = 'PROPERTY_REMOVED', // 属性移除
  PROPERTY_VALIDATED = 'PROPERTY_VALIDATED', // 属性验证

  // 能力管理事件
  CAPABILITY_ADDED = 'CAPABILITY_ADDED', // 能力添加
  CAPABILITY_REMOVED = 'CAPABILITY_REMOVED', // 能力移除
  CAPABILITY_UPDATED = 'CAPABILITY_UPDATED', // 能力更新
  CAPABILITY_EXECUTED = 'CAPABILITY_EXECUTED', // 能力执行

  // 交易事件
  TRADE_CREATED = 'TRADE_CREATED', // 交易创建
  TRADE_COMPLETED = 'TRADE_COMPLETED', // 交易完成
  TRADE_CANCELLED = 'TRADE_CANCELLED', // 交易取消
  TRADE_FAILED = 'TRADE_FAILED', // 交易失败

  // 监控告警事件
  ALERT_TRIGGERED = 'ALERT_TRIGGERED', // 告警触发
  ALERT_RESOLVED = 'ALERT_RESOLVED', // 告警解决
  ALERT_ESCALATED = 'ALERT_ESCALATED', // 告警升级

  // 系统事件
  SYSTEM_STARTUP = 'SYSTEM_STARTUP', // 系统启动
  SYSTEM_SHUTDOWN = 'SYSTEM_SHUTDOWN', // 系统关闭
  SYSTEM_ERROR = 'SYSTEM_ERROR', // 系统错误
  SYSTEM_MAINTENANCE = 'SYSTEM_MAINTENANCE', // 系统维护

  // 自定义事件
  CUSTOM_EVENT = 'CUSTOM_EVENT', // 自定义事件
  USER_ACTION = 'USER_ACTION', // 用户操作
  EXTERNAL_WEBHOOK = 'EXTERNAL_WEBHOOK', // 外部Webhook
}

// 事件严重程度枚举
export enum EventSeverity {
  DEBUG = 'DEBUG', // 调试
  INFO = 'INFO', // 信息
  WARNING = 'WARNING', // 警告
  ERROR = 'ERROR', // 错误
  CRITICAL = 'CRITICAL', // 严重
}

// 事件来源枚举
export enum EventSource {
  SYSTEM = 'SYSTEM', // 系统
  USER = 'USER', // 用户
  API = 'API', // API
  SCHEDULER = 'SCHEDULER', // 调度器
  EXTERNAL = 'EXTERNAL', // 外部
  BEHAVIOR = 'BEHAVIOR', // 行为
  COMPENSATION = 'COMPENSATION', // 补偿
  MONITOR = 'MONITOR', // 监控
}

// 事件定义接口
export interface EventDefinition {
  id: string;
  name: string;
  code: string;
  type: EventType;
  severity: EventSeverity;
  source: EventSource;
  description?: string;
  resourceType?: string;
  parameters: EventParameter[];
  conditions: EventCondition[];
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}

// 事件参数接口
export interface EventParameter {
  name: string;
  type: 'string' | 'number' | 'boolean' | 'object' | 'array';
  required: boolean;
  defaultValue?: any;
  description?: string;
  validation?: {
    min?: number;
    max?: number;
    pattern?: string;
    enum?: string[];
  };
}

// 事件条件接口
export interface EventCondition {
  id: string;
  field: string;
  operator:
    | 'eq'
    | 'ne'
    | 'gt'
    | 'gte'
    | 'lt'
    | 'lte'
    | 'in'
    | 'nin'
    | 'contains'
    | 'regex';
  value: any;
  logicalOperator?: 'and' | 'or';
}

// 事件实例接口
export interface Event {
  id: string;
  definitionId: string;
  definitionName: string;
  type: EventType;
  severity: EventSeverity;
  source: EventSource;
  resourceId: string;
  resourceType: string;
  resourceName: string;
  payload: Record<string, any>;
  metadata: Record<string, any>;
  timestamp: string;
  processed: boolean;
  processedAt?: string;
  subscribers: EventSubscriber[];
  tags: string[];
}

// 事件订阅者接口
export interface EventSubscriber {
  id: string;
  name: string;
  type: 'webhook' | 'email' | 'sms' | 'notification' | 'function';
  target: string;
  parameters: Record<string, any>;
  enabled: boolean;
  lastTriggered?: string;
  successCount: number;
  failureCount: number;
}

// 事件过滤器接口
export interface EventFilter {
  id: string;
  name: string;
  description?: string;
  eventTypes: EventType[];
  resourceTypes?: string[];
  severityLevels?: EventSeverity[];
  timeRange?: {
    start: string;
    end: string;
  };
  conditions: EventCondition[];
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}

// 事件查询接口
export interface EventQuery {
  page?: number;
  pageSize?: number;
  eventTypes?: EventType[];
  resourceTypes?: string[];
  resourceIds?: string[];
  severityLevels?: EventSeverity[];
  sources?: EventSource[];
  startTime?: string;
  endTime?: string;
  processed?: boolean;
  tags?: string[];
  keyword?: string;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

// 事件统计接口
export interface EventStatistics {
  totalEvents: number;
  eventsByType: Array<{
    type: EventType;
    count: number;
    percentage: number;
  }>;
  eventsBySeverity: Array<{
    severity: EventSeverity;
    count: number;
    percentage: number;
  }>;
  eventsBySource: Array<{
    source: EventSource;
    count: number;
    percentage: number;
  }>;
  eventsByTime: Array<{
    time: string;
    count: number;
  }>;
  processedEvents: number;
  unprocessedEvents: number;
  processingRate: number;
}

// 事件触发器接口
export interface EventTrigger {
  id: string;
  name: string;
  description?: string;
  eventType: EventType;
  conditions: EventCondition[];
  actions: EventAction[];
  enabled: boolean;
  schedule?: {
    cron: string;
    timezone: string;
  };
  createdAt: string;
  updatedAt: string;
}

// 事件动作接口
export interface EventAction {
  id: string;
  name: string;
  type: 'webhook' | 'email' | 'sms' | 'notification' | 'function' | 'behavior';
  target: string;
  parameters: Record<string, any>;
  order: number;
  enabled: boolean;
  retryCount: number;
  retryInterval: number;
}

// 事件处理结果接口
export interface EventProcessingResult {
  eventId: string;
  success: boolean;
  processedAt: string;
  duration: number;
  error?: string;
  actions: Array<{
    actionId: string;
    actionName: string;
    success: boolean;
    result?: any;
    error?: string;
    duration: number;
  }>;
}

// 事件订阅接口
export interface EventSubscription {
  id: string;
  name: string;
  description?: string;
  eventTypes: EventType[];
  resourceTypes?: string[];
  resourceIds?: string[];
  filters: EventFilter[];
  subscribers: EventSubscriber[];
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}

// 事件模板接口
export interface EventTemplate {
  id: string;
  name: string;
  description?: string;
  eventType: EventType;
  severity: EventSeverity;
  source: EventSource;
  payloadTemplate: Record<string, any>;
  metadataTemplate: Record<string, any>;
  tags: string[];
  resourceTypes: string[];
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}
